package components
{
	import components.DecalDisplay;
	import components.ListCombobox;
	
	import container.WindowSelectAuto;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import managers.GlobalDecalSheetManager;
	
	import ru.inils.display.BitmapScale;
	
	public class Combobox extends Sprite
	{
		private var resizeTextButton:		BitmapScale;
		private var resizeTextButtonOver:	BitmapScale;
		private var resizeScroll:			BitmapScale;
		private var slider:					DecalDisplay;
		
		private var containerCombobox:		Sprite;

		private var listMask:				Bitmap;
		
		private var maxVisibleItems:		int;
		private var heightItem:				int;
		private var deltaIndex:				int;
		public var index:					int;
		public var list:					ListCombobox;
		
		private var comboText:				TextField;
		public var defaultText:				String;
		
		public function Combobox(designTextInput:String, designScroll:String, designSelect:String,
								 designSlider:String, designList:String,
								 textFormatList:TextFormat, embedFontsList:Boolean, locationList:Point,
								 textFormatCombo:TextFormat, embedFontsCombo:Boolean, locationCombo:Point, defaultTextIn:String){
			containerCombobox = new Sprite();
			
			defaultText = defaultTextIn;
			var textButton:	DecalDisplay = new DecalDisplay(designTextInput);
			var size:int = textButton.width;
			var rectTextFieldButton: Rectangle = textButton.bitmapData.getColorBoundsRect(0xFFFFFFFF, 0x000000, false);
			textButton.width = rectTextFieldButton.width;
			var RectScale9Grid: Rectangle = GlobalDecalSheetManager.instance.
				getSheet(GlobalDecalSheetManager.instance.decalLookup[textButton.defaultSrc]).decalScile9Rect[textButton.defaultSrc];
			resizeTextButton = new BitmapScale(textButton.bitmapData, RectScale9Grid);
			this.x = textButton.x;
			this.y = textButton.y;
			resizeTextButton.width = size;
			containerCombobox.addChild(resizeTextButton);

			var textButtonOver:	DecalDisplay = new DecalDisplay(designTextInput);
			textButtonOver.src = textButtonOver.defaultSrc + FittingRoom.DecalNameOver;
			textButtonOver.width = rectTextFieldButton.width;
			resizeTextButtonOver = new BitmapScale(textButtonOver.bitmapData, RectScale9Grid);
			resizeTextButtonOver.width = size;
			resizeTextButtonOver.visible = false;
			containerCombobox.addChild(resizeTextButtonOver);
			
			comboText = new TextField();
			comboText.x = locationCombo.x;
			comboText.y = locationCombo.y;
			comboText.width = size - (rectTextFieldButton.width - RectScale9Grid.left - RectScale9Grid.width) - locationCombo.x;
			comboText.height = rectTextFieldButton.height;
			comboText.embedFonts = embedFontsCombo;
			comboText.defaultTextFormat = textFormatCombo;
			comboText.text = defaultText;
			
			containerCombobox.addChild(comboText);
			containerCombobox.mouseChildren = false;
			containerCombobox.buttonMode = true;
			addChild(containerCombobox);
			
			addEventListener(MouseEvent.MOUSE_OVER, ComboboxOver);
			containerCombobox.addEventListener(MouseEvent.MOUSE_DOWN, ComboboxDown);
			
			var select:	DecalDisplay = new DecalDisplay(designSelect);
			heightItem = select.height;
		
			var listX:int = select.x;
			var listY:int = select.y;
			var backgroundList:DecalDisplay = new DecalDisplay(designList);
			if (backgroundList.row < 1) maxVisibleItems = 1
			else maxVisibleItems = backgroundList.row;
			list = new ListCombobox(select, backgroundList.backgroundColor, maxVisibleItems, textFormatList, embedFontsList, locationList);
			list.x = listX
			list.y = listY;
			list.visible = false;
			list.mouseChildren = false;
			addChild(list);
			list.addEventListener(MouseEvent.MOUSE_DOWN, ListDown);
			
			listMask = new Bitmap(new BitmapData(select.width, select.height * maxVisibleItems, true, 0x0F000001));
			addChild(listMask);
			listMask.x = list.x;
			listMask.y = list.y;
			list.mask = listMask;
			
			var scroll:	DecalDisplay = new DecalDisplay(designScroll);
			var rectScroll: Rectangle = scroll.bitmapData.getColorBoundsRect(0xFFFFFFFF, 0x000000, false);
			var RectScale9GridScroll: Rectangle = GlobalDecalSheetManager.instance.
			getSheet(GlobalDecalSheetManager.instance.decalLookup[scroll.defaultSrc]).decalScile9Rect[scroll.defaultSrc];
			resizeScroll = new BitmapScale(scroll.bitmapData, RectScale9GridScroll);
			resizeScroll.x = listMask.x + listMask.width + scroll.x;
			resizeScroll.y = listMask.y + scroll.y;
			resizeScroll.height = listMask.height - scroll.y;
			resizeScroll.visible = false;
			addChild(resizeScroll);
			
			slider = new DecalDisplay(designSlider);
			slider.x = resizeScroll.x + scroll.width / 2 - slider.width / 2;
			slider.y = resizeScroll.y + slider.height / 2;
			slider.visible = false;
			addChild(slider);
			slider.buttonMode = true;
			slider.addEventListener(MouseEvent.MOUSE_DOWN, SliderDown);
			slider.addEventListener(MouseEvent.MOUSE_OVER, ButtonOver);
			
			addEventListener("updateList", updateList);
		}
		private function updateList(e:Event):void{
			comboText.text = defaultText;
			deltaIndex = 0;
			list.allItems.y = 0;
			slider.y = resizeScroll.y + slider.height / 2;
			updateFrameList();
		}
		private function ComboboxOver(e:MouseEvent):void{
			removeEventListener(MouseEvent.MOUSE_OVER, ComboboxOver);
			resizeTextButtonOver.visible = true;
			resizeTextButton.visible = false;
			addEventListener(MouseEvent.MOUSE_OUT, ComboboxOut);
		}
		private function ComboboxOut(e:MouseEvent):void{
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, ComboboxOut);
			resizeTextButton.visible = true;
			resizeTextButtonOver.visible = false;
			e.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, ComboboxOver);
		}
		private function ComboboxDown(e:MouseEvent):void{
			list.visible = !list.visible;
			updateFrameList();
		}
		private function updateFrameList():void{
			if (list.visible) {
				if (list.Data.length != 0){
					if (list.Data.length > maxVisibleItems) {resizeScroll.visible = true;slider.visible = true}
					else {resizeScroll.visible = false;slider.visible = false}
					listMask.height = heightItem * Math.min(list.Data.length, maxVisibleItems);
				} else {listMask.height = heightItem; resizeScroll.visible = false;slider.visible = false}
			} else {resizeScroll.visible = false;slider.visible = false}
		}
		private function ListDown(e:MouseEvent):void{
			resizeScroll.visible = false;
			slider.visible = false;
			list.visible = false;
			if (list.Data.length != 0){
				index = list.index + deltaIndex;
				if (comboText.text != list.Data[index]){
					comboText.text = list.Data[index];
					dispatchEvent(new Event("changeCombobox"));
				}
			}
		}
		private function ButtonOver(e:MouseEvent):void{
			slider.removeEventListener(MouseEvent.MOUSE_OVER, ButtonOver);
			slider.src = slider.defaultSrc + FittingRoom.DecalNameOver;
			slider.addEventListener(MouseEvent.MOUSE_OUT, ButtonOut);
		}
		private function ButtonOut(e:MouseEvent):void{
			slider.removeEventListener(MouseEvent.MOUSE_OUT, ButtonOut);
			slider.src = slider.defaultSrc;
			slider.addEventListener(MouseEvent.MOUSE_OVER, ButtonOver);
		}
		private function SliderDown(e:MouseEvent):void{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, SliderMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, SliderUp);
			slider.removeEventListener(MouseEvent.MOUSE_OUT, ButtonOut);
		}
		private function SliderUp(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, SliderMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, SliderUp);
			ButtonOut(e);
		}
		private function SliderMove(e:MouseEvent):void{
			if (mouseY > (resizeScroll.y + slider.height) &&
				mouseY < (resizeScroll.y + resizeScroll.height - slider.height)) {
				slider.y = mouseY - slider.height / 2;
				deltaIndex = Math.ceil((slider.y - slider.height / 2 - resizeScroll.y) / (resizeScroll.height - 2 * slider.height)
										* (list.Data.length - maxVisibleItems + 1)) - 1;
				list.allItems.y = - deltaIndex * heightItem;
			}
		}
	}
}
